/**
 * Copyright 2011 Declan.z
 */
package com.mail163.email.location;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

/**
 * @author Declan.Z(declan.zhang@gmail.com)
 * @date 2011-3-3
 */
public class LocationUtils {

	private static final double EARTH_RADIUS = 6378.137;
	private static final double PI_OVER_360 = Math.PI / 360;
	private static final double PI_OVER_180 = Math.PI / 180; // 弧度->角度

	// public static final Location createLocation(
	// android.location.Location location) {
	// if (location == null) {
	// return new Location(null, null);
	// }
	//
	// String latitude = null;
	// if (location.getLatitude() != 0.0) {
	// latitude = String.valueOf(location.getLatitude() + Config.FIX_LAT);
	// }
	//
	// String longitude = null;
	// if (location.getLongitude() != 0.0) {
	// longitude = String.valueOf(location.getLongitude() + Config.FIX_LNG);
	// }
	//
	// return new Location(latitude, longitude);
	// }

	public static void openGps(Context context) {
		String str = Settings.Secure.getString(context.getContentResolver(),
				Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
		if (!str.contains("gps")) {
			toggleGps(context);
		}
	}

	public static void toggleGps(Context context) {
		Intent gpsIntent = new Intent();
		gpsIntent.setClassName("com.android.settings",
				"com.android.settings.widget.SettingsAppWidgetProvider");
		gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
		gpsIntent.setData(Uri.parse("custom:3"));
		try {
			PendingIntent.getBroadcast(context, 0, gpsIntent, 0).send();
		} catch (CanceledException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 把字符串形式的1�7 "30.5645643" ,经纬度转化为 mapview可以用的30543322
	 * 
	 * @param str
	 * @return
	 */
	public static int getGeoInteger(double data) {
		return (int) (data * 1e6);
	}

//	public static GeoPoint getGeoPoint(Location location) {
//		return new GeoPoint(getGeoInteger(location.lat),
//				getGeoInteger(location.lng));
//	}
//
//	public static GeoPoint getGeoPoint(double lat, double lng) {
//		return new GeoPoint(getGeoInteger(lat), getGeoInteger(lng));
//	}
//
//	public static GeoPoint getGeoPoint(String lat, String lng) {
//		return getGeoPoint(Double.parseDouble(lat), Double.parseDouble(lng));
//	}

	// public static GeoPoint createGeoPoint(Location location) {
	// return getGeoPoint(createLocation(location));
	// }

	/**
	 * 添加地点时用
	 * 
	 * @param point
	 * @return
	 */
//	public static Location createLocation(GeoPoint point) {
//		String lat = point.getLatitudeE6() * 1f / 1e6 + "";
//		String lng = point.getLongitudeE6() * 1f / 1e6 + "";
//		return new Location(lat, lng);
//	}

	/**
	 * 是否能签刄1�7(超出签到范围)
	 * 
	 * @param place
	 * @param location
	 * @return
	 */
	/*public static boolean isCheckInable(Place place, Location location) {
		if (place == null || place.getLat() == null || place.getLng() == null
				|| location == null) {
			return false;
		}

		double lat1 = Double.parseDouble(place.getLat());
		double lng1 = Double.parseDouble(place.getLng());
		double lat2 = location.lat;
		double lng2 = location.lng;

		return (getDistance(lat1, lng1, lat2, lng2) < Config.CHECK_IN_DISTANCE);
	}*/

	/**
	 * @param lat1
	 * @param lng1
	 * @param lat2
	 * @param lng2
	 * @return 单位:KM
	 */
	public static double getDistance(double lat1, double lng1, double lat2,
			double lng2) {
		double dis = EARTH_RADIUS
				* 2
				* Math.asin(Math.sqrt(Math.pow(Math.sin(lat1 - lat2)
						* PI_OVER_360, 2)
						+ Math.cos(lat1 * PI_OVER_180)
						* Math.cos(lat2 * PI_OVER_180)
						* Math.pow(Math.sin((lng1 - lng2) * PI_OVER_360), 2)));
		return dis;
	}

	/**
	 * 转换m -> px
	 * 
	 * @param meters
	 * @param map
	 * @param latitude
	 * @return
	 */
	// public static int metersToRadius(float meters, MapView map, double
	// latitude) {
	// return (int) (map.getProjection().metersToEquatorPixels(meters) * (1/
	// Math.cos(Math.toRadians(latitude))));
	// }

//	public static int metersToRadius(float meters, MapView map, double latitude) {
//		// return (int) map.getProjection().metersToEquatorPixels(meters);
//		return Math.abs((int) (map.getProjection()
//				.metersToEquatorPixels(meters) / (Math.cos(Math
//				.toRadians(latitude)))));
//	}

}
